package com.controller;


import com.beans.Admin;
import com.beans.Forum;
import com.beans.ResponseResult;
import com.beans.User;
import com.service.ForumService;
import com.vo.CommentVo;
import com.vo.ForumVo;
import com.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/forum")
public class ForumController {

    @Resource
    ForumService forumService;
    @Resource
    CommentService commentService;

    //论坛列表
    @RequestMapping("/toForum")
    public String toForum(Model model){
        List<ForumVo> allForum = forumService.getAllForum();
        for (ForumVo forumVo : allForum) {
            String content = forumVo.getContent();
            if(content.length() >= 100){
                forumVo.setContent(content.substring(0,200)+"...");
            }
            forumVo.setCountComment(commentService.countCommentByForumId(forumVo));
        }
        model.addAttribute("forumInfos", allForum);
        return "forum/forum-list";
    }

    //跳转论坛详情
    @RequestMapping("/toForumDetail")
    public String toForumDetail(@RequestParam("id")int id, Model model, HttpSession session){
        User userInfo = (User)session.getAttribute("userInfo");
        if (userInfo != null){
            model.addAttribute("userInfoId", userInfo.getId().intValue());
        }

        //selectById
        ForumVo forumVo = forumService.selectForumById(id);
        forumVo.setCountComment(commentService.countCommentByForumId(forumVo));
        List<CommentVo> commentVos = commentService.listByForumId(id);

        model.addAttribute("forumInfo", forumVo);
        model.addAttribute("commentInfos", commentVos);
        return "forum/forum-detail";
    }


    /***************** 【以下为修改部分内容】 *********************/


    @RequestMapping("/getAllForum")
    public String getAllForum(){
        return "forum/all-forum";
    }

    @RequestMapping("/queryByPage")
    @ResponseBody
    public Map<String, Object> queryByPage(Forum forum, Integer page, Integer limit) {
        return forumService.queryByPage(forum, page, limit);
    }

    @RequestMapping("/deleteForum")
    @ResponseBody
    public Integer deleteForum(int id){
        return forumService.deleteForum(id);
    }

    @RequestMapping("/toUpdateForum")
    @ResponseBody
    public Integer toUpdateForum(@RequestBody Forum forum){
        return forumService.updateForum(forum);
    }

    @RequestMapping("/addForum")
    @ResponseBody
    public Integer addForum(@RequestBody Forum forum, HttpSession request){
        Admin admin = (Admin)request.getAttribute("userInfo");
        Long id = admin.getId();
        forum.setAdminId(id.intValue());
        forum.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return forumService.addForum(forum);
    }

}
